package com.zzg.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzg.srb.common.exception.Assert;
import com.zzg.srb.common.result.ResponseEnum;
import com.zzg.srb.core.entity.pojo.UserBind;
import com.zzg.srb.core.entity.pojo.UserInfo;
import com.zzg.srb.core.entity.vo.UserBindVo;
import com.zzg.srb.core.enums.UserBindEnum;
import com.zzg.srb.core.hfb.FormHelper;
import com.zzg.srb.core.hfb.HfbConst;
import com.zzg.srb.core.hfb.RequestHelper;
import com.zzg.srb.core.mapper.UserBindMapper;
import com.zzg.srb.core.mapper.UserInfoMapper;
import com.zzg.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author zhengzg
 * @since 2022-05-27
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVo userBindVO, Long userId) {

        //用户不同，身份证好相同，不允许绑定
        LambdaQueryWrapper<UserBind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(UserBind::getIdCard,userBindVO.getIdCard())
                .ne(UserBind::getUserId,userId);
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

        //检查用户是否绑定
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBind::getUserId,userId);
        userBind = baseMapper.selectOne(queryWrapper);

        if (ObjectUtils.isEmpty(userBind)){
            //创建用户绑定记录
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVO,userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            baseMapper.insert(userBind);
        }else {
            BeanUtils.copyProperties(userBindVO,userBind);
            baseMapper.updateById(userBind);
        }

        //生成动态表单字符串
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId",HfbConst.AGENT_ID);
        paramMap.put("agentUserId",userId);
        paramMap.put("idCard",userBindVO.getIdCard());
        paramMap.put("personalName", userBindVO.getName());
        paramMap.put("bankType", userBindVO.getBankType());
        paramMap.put("bankNo", userBindVO.getBankNo());
        paramMap.put("mobile", userBindVO.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        return FormHelper.buildForm(HfbConst.USERBIND_URL,paramMap);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notify(Map<String, Object> paramMap) {

        String bindCode = (String) paramMap.get("bindCode");
        String agentUserId = (String) paramMap.get("agentUserId");

        LambdaQueryWrapper<UserBind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBind::getUserId,agentUserId);

        //更新用户绑定表
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.updateById(userBind);

        //更新用户表
        UserInfo userInfo = userInfoMapper.selectById(agentUserId);
        userInfo.setBindCode(bindCode);
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfoMapper.updateById(userInfo);

    }

    @Override
    public String getBindCodeByUserId(Long userId) {
        LambdaQueryWrapper<UserBind> userBindQueryWrapper = new LambdaQueryWrapper<>();
        userBindQueryWrapper.eq(UserBind::getUserId, userId);
        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
        String bindCode = userBind.getBindCode();
        return bindCode;
    }
}
